package com.hua.yuan.fragment;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.google.gson.Gson;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.base.ITabFragment;
import com.hua.yuan.ui.ImageShowActivity;
import com.hua.yuan.ui.data.PictureDataRequest;
import com.hua.yuan.ui.module.ImageItem;
import com.hua.yuan.ui.module.PicBannerBean;
import com.hua.yuan.view.ImageBannerView;
import com.hua.yuan.view.ImageItemView;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import cm.hua.yuan.common.utils.AppContextManager;
import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;
/**
 * Created by huangyang on 2018/5/8.
 * 图片的fragment
 */

public class ImageFragment   extends Fragment implements  ITabFragment,View.OnClickListener {

    private RelativeLayout videoSearchRl;

    private ImageBannerView myBannerView;

    private LinearLayout videoTwoLl1;

    private LinearLayout videoTwoLl2;

    private LinearLayout videoTwoLl3;

    private LinearLayout videoTwoLl4;

    private ImageItemView itemViewHot;
    private ImageItemView itemViewPay;
    private ImageItemView itemViewMonth;
    private ImageItemView itemViewTj;

    private List<ImageItem> hotList;
    private List<ImageItem> payList;
    private List<ImageItem> monthList;
    private List<ImageItem> tJList;

    private String testImage = "https://s3.sinaimg.cn/mw690/002CCsUQgy6F8Wee4G6f2&690";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_image, null);
        initView(view);

        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        hotList = new ArrayList<>();
        payList = new ArrayList<>();
        monthList = new ArrayList<>();
        tJList = new ArrayList<>();

        initData();
    }

    private void initView(View view) {

        videoSearchRl = view.findViewById(R.id.videoSearchRl);
        myBannerView = view.findViewById(R.id.myBannerView);

        videoTwoLl1 = view.findViewById(R.id.videoTwoLl1);
        videoTwoLl2 = view.findViewById(R.id.videoTwoLl2);
        videoTwoLl3 = view.findViewById(R.id.videoTwoLl3);
        videoTwoLl4 = view.findViewById(R.id.videoTwoLl4);

        itemViewHot = view.findViewById(R.id.itemViewHot);
        itemViewPay = view.findViewById(R.id.itemViewPay);
        itemViewMonth = view.findViewById(R.id.itemViewMonth);
        itemViewTj = view.findViewById(R.id.itemViewTj);


    }


    private void initData() {

        videoSearchRl.setOnClickListener(this);
        videoTwoLl1.setOnClickListener(this);
        videoTwoLl2.setOnClickListener(this);
        videoTwoLl3.setOnClickListener(this);
        videoTwoLl4.setOnClickListener(this);

        createData();


    }

    /**
     * 构造数据
     */
    private void createData() {

        String token = PreferenceUtils.getString(Constents.TOKEN, "");

        PictureDataRequest.getPictureIndex(token, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");
                        if ("0".equals(code)) {

                            if (!jsonObject.isNull("data")) {
                                JSONObject dataJson = jsonObject.getJSONObject("data");

                                if (!dataJson.isNull("hot")) {
                                    //热门
                                    JSONArray hotJson = dataJson.getJSONArray("hot");

                                    Gson gson = new Gson();

                                    if (hotList == null) {
                                        hotList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < hotJson.length(); i++) {
                                        JSONObject hot = hotJson.getJSONObject(i);
                                        ImageItem bean = gson.fromJson(hot.toString(), ImageItem.class);

                                        if (bean != null) {
                                            hotList.add(bean);
                                        }
                                    }

                                    if (itemViewHot != null) {
                                        itemViewHot.setData(hotList, 0);
                                    }

                                }

                                if (!dataJson.isNull("pay")) {
                                    //付费

                                    JSONArray payJson = dataJson.getJSONArray("pay");

                                    Gson gson = new Gson();

                                    if (payList == null) {
                                        payList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < payJson.length(); i++) {
                                        JSONObject pay = payJson.getJSONObject(i);
                                        ImageItem bean = gson.fromJson(pay.toString(), ImageItem.class);
                                        if (bean != null) {
                                            payList.add(bean);
                                        }
                                    }

                                    if (itemViewPay != null) {
                                        itemViewPay.setData(payList, 1);
                                    }

                                }
                                if (!dataJson.isNull("month")) {
                                    //包月

                                    JSONArray monthJson = dataJson.getJSONArray("month");

                                    Gson gson = new Gson();

                                    if (monthList == null) {
                                        monthList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < monthJson.length(); i++) {
                                        JSONObject month = monthJson.getJSONObject(i);
                                        ImageItem bean = gson.fromJson(month.toString(), ImageItem.class);
                                        if (bean != null) {
                                            monthList.add(bean);
                                        }
                                    }

                                    if (itemViewMonth != null) {
                                        itemViewMonth.setData(monthList, 2);
                                    }


                                }
                                if (!dataJson.isNull("special")) {
                                    //特价
                                    JSONArray tJJson = dataJson.getJSONArray("special");
                                    Gson gson = new Gson();
                                    if (tJList == null) {
                                        tJList = new ArrayList<>();
                                    }
                                    for (int i = 0; i < tJJson.length(); i++) {
                                        JSONObject tj = tJJson.getJSONObject(i);
                                        ImageItem bean = gson.fromJson(tj.toString(), ImageItem.class);
                                        if (bean != null) {
                                            tJList.add(bean);
                                        }
                                    }

                                    if (itemViewTj != null) {
                                        itemViewTj.setData(tJList, 3);
                                    }

                                }

                            }


                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });

        PictureDataRequest.getPictureRot(token, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");
                        if ("0".equals(code)) {

                            if (!jsonObject.isNull("data")) {

                                JSONArray jsonArray = jsonObject.getJSONArray("data");

                                Gson gson = new Gson();
                                List<PicBannerBean> list = new ArrayList<>();

                                for (int i = 0; i < jsonArray.length(); i++) {

                                    JSONObject object = jsonArray.getJSONObject(i);
                                    PicBannerBean bean = gson.fromJson(object.toString(), PicBannerBean.class);
                                    list.add(bean);

                                }

                                if (myBannerView != null) {
                                    myBannerView.setData(list);
                                }

                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });


        //轮播图
//        List<MovieRotBean> list = new ArrayList<>();
//        for (int i = 0; i < 5; i++) {
//            MovieRotBean bean = new MovieRotBean();
//            bean.setId(String.valueOf(i));
//            bean.setImg_url(testImage);
//            list.add(bean);
//        }
//        myBannerView.setData(list);

        //热门推荐
//        ImageBean imageBean1 = new ImageBean();
//        imageBean1.setType(0);
//        List<ImageBean.ImageItem> list1 = new ArrayList<>();
//        for (int i = 0; i < 6; i++) {
//            ImageBean.ImageItem bean = new ImageBean.ImageItem();
//            bean.setImageId(String.valueOf(i));
//            bean.setImageUrl(testImage);
//            bean.setRedu(String.valueOf(100 + i));
//            bean.setImageNum(String.valueOf(10 + i));
//            bean.setDaibi(String.valueOf(100 + i));
//            bean.setImageName("美女图片" + i);
//            bean.setDaibi1(String.valueOf(200 + i));
//            bean.setDaibi2(String.valueOf(100 + i));
//            list1.add(bean);
//        }
//
//        imageBean1.setList(list1);
//        itemViewHot.setData(imageBean1);
//
//        //付费精选
//
//        ImageBean imageBean2 = new ImageBean();
//        imageBean2.setType(1);
//        List<ImageBean.ImageItem> list2 = new ArrayList<>();
//        for (int i = 0; i < 6; i++) {
//            ImageBean.ImageItem bean = new ImageBean.ImageItem();
//            bean.setImageId(String.valueOf(i));
//            bean.setImageUrl(testImage);
//            bean.setRedu(String.valueOf(100 + i));
//            bean.setImageNum(String.valueOf(10 + i));
//            bean.setDaibi(String.valueOf(100 + i));
//            bean.setImageName("美女图片" + i);
//            bean.setDaibi1(String.valueOf(200 + i));
//            bean.setDaibi2(String.valueOf(100 + i));
//            list2.add(bean);
//        }
//
//        imageBean2.setList(list2);
//        itemViewPay.setData(imageBean2);
//
//        //包月精选
//
//        ImageBean imageBean3 = new ImageBean();
//        imageBean3.setType(2);
//        List<ImageBean.ImageItem> list3 = new ArrayList<>();
//        for (int i = 0; i < 6; i++) {
//            ImageBean.ImageItem bean = new ImageBean.ImageItem();
//            bean.setImageId(String.valueOf(i));
//            bean.setImageUrl(testImage);
//            bean.setRedu(String.valueOf(100 + i));
//            bean.setImageNum(String.valueOf(10 + i));
//            bean.setDaibi(String.valueOf(100 + i));
//            bean.setImageName("美女图片" + i);
//            bean.setDaibi1(String.valueOf(200 + i));
//            bean.setDaibi2(String.valueOf(100 + i));
//            list3.add(bean);
//        }
//
//        imageBean3.setList(list3);
//        itemViewMonth.setData(imageBean3);
//
//        //69特价
//
//        ImageBean imageBean4 = new ImageBean();
//        imageBean4.setType(3);
//        List<ImageBean.ImageItem> list4 = new ArrayList<>();
//        for (int i = 0; i < 6; i++) {
//            ImageBean.ImageItem bean = new ImageBean.ImageItem();
//            bean.setImageId(String.valueOf(i));
//            bean.setImageUrl(testImage);
//            bean.setRedu(String.valueOf(100 + i));
//            bean.setImageNum(String.valueOf(10 + i));
//            bean.setDaibi(String.valueOf(100 + i));
//            bean.setImageName("美女图片" + i);
//            bean.setDaibi1(String.valueOf(200 + i));
//            bean.setDaibi2(String.valueOf(100 + i));
//            list4.add(bean);
//        }
//
//        imageBean4.setList(list4);
//        itemViewTj.setData(imageBean4);


    }

    @Override
    public void onClick(View view) {

        Intent intent =new Intent(getContext(),ImageShowActivity.class);
        switch (view.getId()) {
            case R.id.videoSearchRl:
                intent.putExtra("type", 0);
                break;
            case R.id.videoTwoLl1:
                intent.putExtra("type", 0);
                break;
            case R.id.videoTwoLl2:
                intent.putExtra("type", 1);
                break;
            case R.id.videoTwoLl3:
                intent.putExtra("type", 2);
                break;
            case R.id.videoTwoLl4:
                intent.putExtra("type", 3);
                break;
        }
        startActivity(intent);
    }

    @Override
    public String getTabName() {
        //"视频"
        return AppContextManager.getInstance().getContext().getResources().getString(R.string.image);
    }

    @Override
    public int getDrawableId() {
        return R.mipmap.main_image_unselect;
    }

    @Override
    public int getDrawableSelectId() {
        return R.mipmap.main_image_select;
    }

    @Override
    public Fragment getTabFragment() {
        return this;
    }
}
